package com.riko.comic.core.config;

import com.riko.comic.core.converter.LocalDateTimeConverter;
import com.riko.comic.core.interceptor.LoginInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.*;
import org.springframework.format.FormatterRegistry;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

/**
 * spring配置
 *
 * @author riko
 * @since 2021/10/29
 */
@Configuration
@ComponentScan({"com.riko"})
@MapperScan({"com.riko.**.mapper"})
@PropertySource("classpath:application.properties")
@Import({JdbcConfig.class, MyBatisPlusConfig.class, TransactionConfig.class})
@EnableAspectJAutoProxy
@EnableTransactionManagement
@EnableWebMvc
public class SpringConfiguration implements WebMvcConfigurer {

    /**
     * 配置静态资源处理方式二：springmvc处理静态资源
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/pic/**","/resources/**")
                .addResourceLocations("/pic/","/resources/");
    }

    /**
     * 配置commons-upload上传
     */
    @Bean
    public CommonsMultipartResolver multipartResolver(){
        CommonsMultipartResolver resolver = new CommonsMultipartResolver();
        //设置限制上传大小
        resolver.setMaxUploadSize(104857600);
        //设置编码
        resolver.setDefaultEncoding("UTF-8");
        return resolver;
    }


    /**
     * 添加拦截器
     * addInterceptor方法指定自定义拦截器对象
     * addPathPatterns方法指定哪些请求经过拦截器
     * excludePathPatterns方法指定哪些请求放行
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoginInterceptor())
                .addPathPatterns("/comic/**/chapter/**","/comic/**/comment","/chapterComments/comments/**/comment"
                        ,"/rank/save","/collect/save","/sys/user/person/**");
        //
        //registry.addInterceptor(new RoleInterceptor())
        //        .addPathPatterns("/item/itemEdit","/user/userEdit");
    }

    /**
     * 配置默认的视图解析器
     */
    @Bean
    public InternalResourceViewResolver viewResolver(){
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setPrefix("/WEB-INF/jsp/");
        viewResolver.setSuffix(".jsp");
        //如果页面需要使用JSTL标签库
        viewResolver.setViewClass(JstlView.class);
        return viewResolver;
    }

    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addConverter(new LocalDateTimeConverter());
        WebMvcConfigurer.super.addFormatters(registry);
    }
}
